<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
const treeData = [
    {
        name: "1",
        children: [
            {
                name: "2"
            },
            {
                name: "3",
                children: [
                    {
                        name: "4"
                    }
                ]
            }
        ]
    }
]
const lastLayerNodeSum = (treeArr) => {
    const maxLayerNum = 1; // 初始层数为1层
    let result = 0; // 最深层的数组结点个数
    const dfs = (treeArr, layerNum) => { // 统计第layerNum层的结点个数，例如lastLayerNodeSum的输入数组就是第一层
        if(layerNum === maxLayerNum) { // 如果是当前与最深层同深，可以挑战一下result
            result = treeArr.length > result ? treeArr.length : result;
        } else if(layerNum > maxLayerNum) { // 如果当前是最深的深度，就直接更新result
            result = treeArr.length;
        }
        for(let i = 0; i < treeArr.length; i ++) {
            if(treeArr[i].hasOwnProperty("children")) {
                dfs(treeArr[i].children, layerNum + 1);
            }
        }
    }
    dfs(treeArr, 1);
    console.log(result);
}
lastLayerNodeSum(treeData);
</script>
</body>
</html>